package com.keon.jianbao.adapter;

import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.keon.jianbao.AppContext;
import com.keon.jianbao.AppManager;
import com.keon.jianbao.R;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.base.SimpleAdapter;
import com.keon.jianbao.bean.Appraisal;
import com.keon.jianbao.util.DialogHelp;
import com.keon.jianbao.util.UILUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by keon on 2015/10/8.
 */
public class JianBaoPayListAdapter extends SimpleAdapter {

    public JianBaoPayListAdapter(ArrayList data) {
        initData(data);
    }

    static class ViewHolder {
        @Bind(R.id.jianbao_item_delete)
        LinearLayout cbDelete;
        @Bind(R.id.jianbao_item_img)
        ImageView sdvImg;
        @Bind(R.id.jianbao_item_type)
        TextView tvType;
        @Bind(R.id.jianbao_item_is_fake)
        TextView tvIsFake;
        @Bind(R.id.jianbao_item_name)
        TextView tvName;
        @Bind(R.id.jianbao_item_evaluate)
        TextView tvEvaluate;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    @Override
    protected View getRealView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = getLayoutInflater(parent.getContext()).inflate(
                    R.layout.list_cell_jianbao_item, null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        Appraisal appraisal = (Appraisal) mDatas.get(position);

//        Uri uri = Uri.parse("file://" + appraisal.getAppraisalNameImages().get(0));
//        vh.sdvImg.setImageURI(uri);
        ImageLoader.getInstance().displayImage("file://" + appraisal.getAppraisalNameImages().get(0), vh.sdvImg, UILUtils.getOptions2());
        if (appraisal.isShowDelBtn()) {
            vh.cbDelete.setVisibility(View.VISIBLE);
            vh.cbDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DialogHelp.getConfirmDialog(AppManager.getAppManager().currentActivity(), AppContext.getResource().getString(R.string.jianbao_pay_edit_delete_confirm), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            if (mDatas.size() > 1) {
                                mDatas.remove(position);
                                notifyDataSetChanged();
                            } else {
                                ((BaseActivity) AppManager.getAppManager().currentActivity()).showToast(R.string.jianbao_pay_edit_delete_error_last);
                            }

                        }
                    }).show();
                }
            });
        } else {
            vh.cbDelete.setVisibility(View.GONE);
        }
        vh.tvIsFake.setVisibility(View.GONE);
        vh.tvType.setText(appraisal.getAppraisalCatlog());
        vh.tvName.setText(appraisal.getAppraisalName());
        vh.tvEvaluate.setText(appraisal.getAppraisalDesc());

        return convertView;
    }

    public void showDelBtn(boolean show) {
        for (int i = 0; i < mDatas.size(); i++) {
            Appraisal appraisal = (Appraisal) mDatas.get(i);
            appraisal.setShowDelBtn(show);
        }
        notifyDataSetChanged();
    }
}
